package main

import (
	"02-grpc/hello"
	"context"
	"log"
	"time"

	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
)

func main() {
	// 构建连接
	conn, err := grpc.NewClient("0.0.0.0:8088", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("无法连接： %v", err)
	}

	defer conn.Close()

	// 生成对应client
	c := hello.NewHelloRpcClient(conn)

	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()

	// 调用方法
	r, err := c.HelloWorld(ctx, &hello.Hello{Name: "QDGZ"})
	if err != nil {
		log.Fatalf("无法初始化: %v", err)
	}
	log.Printf("Greeting: %s", r.GetReply())
}
